﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ColorBox4Sitecore.Colorbox;
using ColorBox4Sitecore.Helpers;
using System.Text;

namespace ColorBox4Sitecore.UI.sitecore_modules.Web.Colorbox4Sitecore.UserControls
{
    /// <summary>
    /// This control is the generic one. 
    /// You may use it even if you are not in a sitecore context. 
    /// You just need to 
    ///  - set the Config property correctly.
    ///  - Register the JS and CSS like this:
    ///      <script type="text/JavaScript" src="/sitecore modules/Web/Colorbox4Sitecore/design/js/jquery-1.6.4.min.js"></script>
    ///      <script type="text/JavaScript" src="/sitecore modules/Web/Colorbox4Sitecore/design/js/jquery.colorbox.js"></script>
    ///      <script type="text/JavaScript" src="/sitecore modules/Web/Colorbox4Sitecore/design/js/customColorBoxV2.js"></script>
    ///      <link rel="stylesheet" type="text/css" href="/sitecore modules/Web/Colorbox4Sitecore/design/css/colorbox.css" />
    ///      <link rel="stylesheet" type="text/css" href="/sitecore modules/Web/Colorbox4Sitecore/design/css/jquery.ui.theme.css" />
    /// </summary>
    public partial class ColorboxControl : System.Web.UI.UserControl
    {
        public Colorbox.Colorbox Config { get; set; }
        public Colorbox.Colorbox DefaultValues { get; set; } 

        public string JsTransition { get { return ColorboxTransitionHelper.ToJsString(Config.Transition); } }
        public string JsWidth { get { return GetFormatedUnit(Config.Width, Config.WidthUnit); } }
        public string JsHeight { get { return GetFormatedUnit(Config.Height, Config.HeightUnit); } }
        public string JsInnerWidth { get { return GetFormatedUnit(Config.InnerWidth, Config.InnerWidthUnit); } }
        public string JsInnerHeight { get { return GetFormatedUnit(Config.InnerHeight, Config.InnerHeightUnit); } }
        public string JsInitialWidth { get { return GetFormatedUnit(Config.InitialWidth, Config.InitialWidthUnit); } }
        public string JsInitialHeight { get { return GetFormatedUnit(Config.InitialHeight, Config.InitialHeightUnit); } }
        public string JsMaxWidth { get { return GetFormatedUnit(Config.MaxWidth, Config.MaxWidthUnit); } }
        public string JsMaxHeight { get { return GetFormatedUnit(Config.MaxHeight, Config.MaxHeightUnit); } }
        public string JsTop { get { return GetFormatedUnit(Config.Top, Config.TopUnit); } }
        public string JsBottom { get { return GetFormatedUnit(Config.Bottom, Config.BottomUnit); } }
        public string JsLeft { get { return GetFormatedUnit(Config.Left, Config.LeftUnit); } }
        public string JsRight { get { return GetFormatedUnit(Config.Right, Config.RightUnit); } }

        protected void Page_Load(object sender, EventArgs e)
        {
            
            LoadPictures();
        }

        private void LoadPictures()
        {
            lvPictures.DataSource = Config.SlideshowPictures;
            lvPictures.DataBind();
        }

        public string GetFormatedUnit(int? value, ColorboxUnit unit)
        {
            if (value.HasValue)
                return string.Format("{0}{1}", value, ColorboxUnitHelper.ToJsString(unit));
            else
                return "false";
        }

        public string ToBoolJs(bool val)
        {
            return val ? "true" : "false";
        }

        public string ColorboxScript
        {
            get
            {
                StringBuilder sb = new StringBuilder();
                sb.AppendFormat("selector : '{0}',{1}", Config.SelectorCssClass, Environment.NewLine);
                
                if(Config.SlideshowPictures != null && Config.SlideshowPictures.Any())
                    sb.AppendFormat("rel : 'pics_{0}',{1}", Config.SelectorCssClass, Environment.NewLine);

                if(Config.Transition != ColorboxDefaultValues.Transition)
                    sb.AppendFormat("transition : '{0}',{1}", JsTransition, Environment.NewLine);

                if(Config.Speed != ColorboxDefaultValues.Speed)
                    sb.AppendFormat("speed : '{0}',{1}", Config.Speed, Environment.NewLine);
                
                if(Config.ScalePhotos != ColorboxDefaultValues.ScalePhotos)
                    sb.AppendFormat("scalePhotos : '{0}',{1}", ToBoolJs(Config.ScalePhotos), Environment.NewLine);

                if(Config.Scrolling != ColorboxDefaultValues.Scrolling)
                    sb.AppendFormat("scrolling : '{0}',{1}", ToBoolJs(Config.Scrolling), Environment.NewLine);

                if(Config.Href != ColorboxDefaultValues.Href)
                    sb.AppendFormat("href : '{0}',{1}", Server.HtmlEncode(Config.Href), Environment.NewLine);

                if(Config.Title != ColorboxDefaultValues.Title)
                    sb.AppendFormat("title : '{0}',{1}", Config.Title, Environment.NewLine);

                if(Config.Rel != ColorboxDefaultValues.Rel)
                    sb.AppendFormat("rel : '{0}',{1}", Config.Rel, Environment.NewLine);

                if(Config.Current != ColorboxDefaultValues.Current)
                    sb.AppendFormat("current : '{0}',{1}", Config.Current, Environment.NewLine);
                
                if(Config.Previous != ColorboxDefaultValues.Previous)
                    sb.AppendFormat("previous : '{0}',{1}", Config.Previous, Environment.NewLine);

                if(Config.Next != ColorboxDefaultValues.Next)
                    sb.AppendFormat("next : '{0}',{1}", Config.Next, Environment.NewLine);

                if(Config.Close != ColorboxDefaultValues.Close)
                    sb.AppendFormat("close : '{0}',{1}", Config.Close, Environment.NewLine);

                if(Config.XhrError != ColorboxDefaultValues.XhrError)
                    sb.AppendFormat("xhrError : '{0}',{1}", Config.XhrError, Environment.NewLine);

                if(Config.ImgError != ColorboxDefaultValues.ImgError)
                    sb.AppendFormat("imgError : '{0}',{1}", Config.ImgError, Environment.NewLine);

                if(Config.Opacity != ColorboxDefaultValues.Opacity)
                    sb.AppendFormat("opacity : '{0}',{1}", Config.Opacity, Environment.NewLine);

                if(Config.Open != ColorboxDefaultValues.Open)
                    sb.AppendFormat("open : '{0}',{1}", ToBoolJs(Config.Open), Environment.NewLine);

                if(Config.ReturnFocus != ColorboxDefaultValues.ReturnFocus)
                    sb.AppendFormat("returnFocus : '{0}',{1}", ToBoolJs(Config.ReturnFocus), Environment.NewLine);

                if(Config.FastIframe != ColorboxDefaultValues.FastIframe)
                    sb.AppendFormat("fastIframe : '{0}',{1}", ToBoolJs(Config.FastIframe), Environment.NewLine);

                if(Config.Preloading != ColorboxDefaultValues.Preloading)
                    sb.AppendFormat("preloading : '{0}',{1}", ToBoolJs(Config.Preloading), Environment.NewLine);

                if(Config.OverlayClose != ColorboxDefaultValues.OverlayClose)
                    sb.AppendFormat("overlayClose : '{0}',{1}", ToBoolJs(Config.OverlayClose), Environment.NewLine);

                if(Config.EscKey != ColorboxDefaultValues.EscKey)
                    sb.AppendFormat("escKey : '{0}',{1}", ToBoolJs(Config.EscKey), Environment.NewLine);

                if(Config.ArrowKey != ColorboxDefaultValues.ArrowKey)
                    sb.AppendFormat("arrowKey : '{0}',{1}", ToBoolJs(Config.ArrowKey), Environment.NewLine);

                if(Config.Loop != ColorboxDefaultValues.Loop)
                    sb.AppendFormat("loop : '{0}',{1}", ToBoolJs(Config.Loop), Environment.NewLine);

                if(Config.Data != ColorboxDefaultValues.Data)
                    sb.AppendFormat("data : '{0}',{1}", ToBoolJs(Config.Data), Environment.NewLine);

                if(Config.Iframe != ColorboxDefaultValues.Iframe)
                    sb.AppendFormat("iframe : '{0}',{1}", ToBoolJs(Config.Iframe), Environment.NewLine);

                if(Config.Inline != ColorboxDefaultValues.Inline)
                    sb.AppendFormat("inline : '{0}',{1}", ToBoolJs(Config.Inline), Environment.NewLine);

                if(Config.Html != ColorboxDefaultValues.Html)
                    sb.AppendFormat("html : '{0}',{1}", Config.Html, Environment.NewLine);

                if(Config.Photo != ColorboxDefaultValues.Photo)
                    sb.AppendFormat("photo : '{0}',{1}", ToBoolJs(Config.Photo), Environment.NewLine);

                if(Config.Width != ColorboxDefaultValues.Width
                    || Config.WidthUnit != ColorboxDefaultValues.WidthUnit)
                    sb.AppendFormat("width : '{0}',{1}", JsWidth, Environment.NewLine);

                if(Config.Height != ColorboxDefaultValues.Height
                    || Config.HeightUnit != ColorboxDefaultValues.HeightUnit)
                    sb.AppendFormat("height : '{0}',{1}", JsHeight, Environment.NewLine);

                if(Config.InnerWidth != ColorboxDefaultValues.InnerWidth
                    || Config.InnerWidthUnit != ColorboxDefaultValues.InnerWidthUnit)
                    sb.AppendFormat("innerWidth : '{0}',{1}", JsInnerWidth, Environment.NewLine);

                if (Config.InnerHeight != ColorboxDefaultValues.InnerHeight
                    || Config.InnerHeightUnit != ColorboxDefaultValues.InnerHeightUnit)
                    sb.AppendFormat("innerHeight : '{0}',{1}", JsInnerHeight, Environment.NewLine);

                if(Config.InitialWidth != ColorboxDefaultValues.InitialWidth
                    || Config.InitialWidthUnit != ColorboxDefaultValues.InitialWidthUnit)
                    sb.AppendFormat("initialWidth : '{0}',{1}", JsInitialWidth, Environment.NewLine);

                if(Config.InitialHeight != ColorboxDefaultValues.InitialHeight
                    || Config.InitialHeightUnit != ColorboxDefaultValues.InitialHeightUnit)
                    sb.AppendFormat("initialHeight : '{0}',{1}", JsInitialHeight, Environment.NewLine);

                if(Config.MaxWidth != ColorboxDefaultValues.MaxWidth
                    || Config.MaxWidthUnit != ColorboxDefaultValues.MaxWidthUnit)
                    sb.AppendFormat("maxWidth : '{0}',{1}", JsMaxWidth, Environment.NewLine);

                if(Config.MaxHeight != ColorboxDefaultValues.MaxHeight
                    || Config.MaxHeightUnit != ColorboxDefaultValues.MaxHeightUnit)
                    sb.AppendFormat("maxHeight : '{0}',{1}", JsMaxHeight, Environment.NewLine);

                if(Config.SlideshowEnable != ColorboxDefaultValues.SlideshowEnable)
                    sb.AppendFormat("slideshowEnable : '{0}',{1}", ToBoolJs(Config.SlideshowEnable), Environment.NewLine);

                if(Config.SlideshowSpeed != ColorboxDefaultValues.SlideshowSpeed)
                    sb.AppendFormat("slideshowSpeed : '{0}',{1}", Config.SlideshowSpeed, Environment.NewLine);

                if(Config.SlideshowAuto != ColorboxDefaultValues.SlideshowAuto)
                    sb.AppendFormat("slideshowAuto : '{0}',{1}", ToBoolJs(Config.SlideshowAuto), Environment.NewLine);

                if(Config.SlideshowStartText != ColorboxDefaultValues.SlideshowStartText)
                    sb.AppendFormat("slideshowStartText : '{0}',{1}", Config.SlideshowStartText, Environment.NewLine);

                if(Config.SlideshowEndText != ColorboxDefaultValues.SlideshowEndText)
                    sb.AppendFormat("slideshowEndText : '{0}',{1}", Config.SlideshowEndText, Environment.NewLine);

                 if(Config.Fixed != ColorboxDefaultValues.Fixed)
                    sb.AppendFormat("fixed : '{0}',{1}", ToBoolJs(Config.Fixed), Environment.NewLine);

                if(Config.Top != ColorboxDefaultValues.Top)
                    sb.AppendFormat("top : '{0}',{1}", JsTop, Environment.NewLine);

                if(Config.Bottom != ColorboxDefaultValues.Bottom)
                    sb.AppendFormat("bottom : '{0}',{1}", JsBottom, Environment.NewLine);

                if(Config.Left != ColorboxDefaultValues.Left)
                    sb.AppendFormat("left : '{0}',{1}", JsLeft, Environment.NewLine);

                if(Config.Right != ColorboxDefaultValues.Right)
                    sb.AppendFormat("right : '{0}',{1}", JsRight, Environment.NewLine);

                if (Config.AdditionalProperties != ColorboxDefaultValues.AdditionalProperties)
                    sb.AppendFormat("{0}{1}", Config.AdditionalProperties, Environment.NewLine);

                return sb.ToString();
            }
        }
    }
}